package br.ufrj.neg.reuniao.model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import br.ufrj.neg.bean.TraducaoBean;
import br.ufrj.neg.model.ServiceLocator;
import br.ufrj.neg.model.TraducaoModel;
import br.ufrj.neg.reuniao.bean.TipoReuniaoBean;

public class TipoReuniaoModel {
	/**
	 * @param trb TipoReuniaoBean com o id do tipo da reuniao setado
	 */
	public TipoReuniaoBean buscaTipoReuniao(TipoReuniaoBean trb) {
		ResultSet rs = null;
		PreparedStatement ps = null;
		Connection conn = ServiceLocator.getConnection();
		try {
			TipoReuniaoBean tipoReuniao = trb.getNewInstance();
			String sql = "Select * from tipo_reuniao where tire_cd_id = ?";
			ps = conn.prepareStatement(sql);
			ps.setInt(1, trb.getId());
			rs = ps.executeQuery();
			while (rs.next()) {
				tipoReuniao.setId(rs.getInt("TIRE_CD_ID"));
				//tipoReuniao.setDescricao(rs.getString("tire_dx_descricao"));
				TraducaoBean traducao = TraducaoModel.getTraducoesElemento(tipoReuniao);			
				tipoReuniao.setDicionario( traducao.getDicionario() );
			}
			return tipoReuniao;
		} catch (Exception e) {
			System.out.println(e);
			return null;
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			try {
				ps.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			try {
				conn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		}

	}

	/**
	 * @param trb TipoReuniaoBean vazio
	 */
	public Collection<TipoReuniaoBean> listaTipoReuniao(TipoReuniaoBean trb) {
		ResultSet rs = null;
		PreparedStatement ps = null;
		Connection conn = ServiceLocator.getConnection();
		try {
			Collection<TipoReuniaoBean> col = new ArrayList<TipoReuniaoBean>();
			TipoReuniaoBean tipoReuniao;
			String sql = "Select * from tipo_reuniao order by tire_cd_id asc";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				tipoReuniao = trb.getNewInstance();
				tipoReuniao.setId(rs.getInt("TIRE_CD_ID"));
				//tipoReuniao.setDescricao(rs.getString("TIRE_TX_DESCRICAO"));
				TraducaoBean traducao = TraducaoModel.getTraducoesElemento(tipoReuniao);			
				tipoReuniao.setDicionario( traducao.getDicionario() );
				col.add(tipoReuniao);
			}
			return col;
		} catch (Exception e) {
			System.out.println(e);
			return null;
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			try {
				ps.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			try {
				conn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		}
	}

	/**
	 * @param trb TipoReuniaoBean vazio
	 */
	public HashMap<Integer, String> mapaTipoReuniao(TipoReuniaoBean trb) {
		ResultSet rs = null;
		PreparedStatement ps = null;
		Connection conn = ServiceLocator.getConnection();
		try {
			HashMap<Integer, String> mapa = new HashMap<Integer, String>();
			String sql = "Select * from tipo_reuniao order by tire_cd_id asc";
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				int id = rs.getInt("TIRE_CD_ID");
				TraducaoBean traducao = TraducaoModel.getTraducoesElemento(trb);
				trb.setDicionario( traducao.getDicionario() );
				String descr = trb.getDescricao();
				mapa.put(id, descr);						
			}
			return mapa;
		} catch (Exception e) {
			System.out.println(e);
			return null;
		} finally {
			try {
				rs.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			try {
				ps.close();
			} catch (Exception e) {
				System.out.println(e);
			}
			try {
				conn.close();
			} catch (Exception e) {
				System.out.println(e);
			}
		}
	}
}
